﻿using AfrikDotnetWebSite.Models;
using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AfrikDotnetWebSite.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        AfrikDotnetContext db = new AfrikDotnetContext();

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult News()
        {
            var news = db.News;
            return View(news);
        }

        public ActionResult NewsAdd()
        {
            return View();
        }

        public ActionResult NewsEdit(int id)
        {
            return View(db.News.Find(id));
        }

        public ActionResult NewsDelete(int id)
        {
            return View(db.News.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewsAdd(News news)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    news.Date = DateTime.UtcNow;
                    db.News.Add(news);
                    db.SaveChanges();
                    return RedirectToAction("news");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewsEdit(News news)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(news).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("news");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost, ActionName("NewsDelete")]
        [ValidateAntiForgeryToken]
        public ActionResult NewsDeleteConfirmed(int id)
        {
            try
            {
                News news = new News() { Id = id };
                db.Entry(news).State = EntityState.Deleted;
                db.SaveChanges();
            }
            catch
            {
                return RedirectToAction("newsdelete",
                    new System.Web.Routing.RouteValueDictionary { 
                        { "id", id }, 
                        { "saveChangesError", true } });
            }

            return RedirectToAction("news");
        }

        public ActionResult Shows()
        {
            var shows = db.Shows.OrderByDescending(s => s.IsActive);
            return View(shows);
        }

        public ActionResult ShowAdd()
        {
            return View();
        }

        public ActionResult ShowEdit(int id)
        {
            return View(db.Shows.Find(id));
        }

        public ActionResult ShowDelete(int id)
        {
            return View(db.Shows.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ShowAdd(Show show, HttpPostedFileBase file)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/shows"), fileName);
                        file.SaveAs(path);
                        show.Image = fileName;
                        db.Shows.Add(show);
                        db.SaveChanges();
                        return RedirectToAction("shows");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Empty file.");
                        return View();
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ShowEdit(Show show, HttpPostedFileBase file)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/shows"), fileName);
                        file.SaveAs(path);
                        show.Image = fileName;
                    }

                    db.Entry(show).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("shows");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost, ActionName("ShowDelete")]
        [ValidateAntiForgeryToken]
        public ActionResult ShowDeleteConfirmed(int id)
        {
            Show show = db.Shows.Find(id);

            try
            {
                var img = show.Image;
                var path = Path.Combine(Server.MapPath("~/static/img/shows"), img);
                System.IO.File.Delete(path);
            }
            catch { }

            try
            {
                db.Shows.Remove(show);
                db.SaveChanges();
            }
            catch
            {
                return RedirectToAction("showdelete",
                    new System.Web.Routing.RouteValueDictionary { 
                        { "id", id }, 
                        { "saveChangesError", true } });
            }

            return RedirectToAction("shows");
        }

        // Project
        public ActionResult Projects()
        {
            var projects = db.Projects;
            return View(projects);
        }

        public ActionResult ProjectAdd()
        {
            return View();
        }

        public ActionResult ProjectEdit(int id)
        {
            return View(db.Projects.Find(id));
        }

        public ActionResult ProjectDelete(int id)
        {
            return View(db.Projects.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ProjectAdd(Project project, HttpPostedFileBase file)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/projects"), fileName);
                        file.SaveAs(path);
                        project.Image = fileName;
                        db.Projects.Add(project);
                        db.SaveChanges();
                        return RedirectToAction("projects");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Empty file.");
                        return View();
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ProjectEdit(Project project, HttpPostedFileBase file)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (file != null && file.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/projects"), fileName);
                        file.SaveAs(path);
                        project.Image = fileName;
                    }

                    db.Entry(project).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("projects");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost, ActionName("ProjectDelete")]
        [ValidateAntiForgeryToken]
        public ActionResult ProjectDeleteConfirmed(int id)
        {
            Project project = db.Projects.Find(id);

            try
            {
                var img = project.Image;
                var path = Path.Combine(Server.MapPath("~/static/img/projects"), img);
                System.IO.File.Delete(path);
            }
            catch { }

            try
            {
                db.Projects.Remove(project);
                db.SaveChanges();
            }
            catch
            {
                return RedirectToAction("projectdelete",
                    new System.Web.Routing.RouteValueDictionary { 
                        { "id", id }, 
                        { "saveChangesError", true } });
            }

            return RedirectToAction("projects");
        }

        // Jobs
        public ActionResult Jobs()
        {
            var jobs = db.Jobs;
            return View(jobs);
        }

        public ActionResult JobAdd()
        {
            return View();
        }

        public ActionResult JobEdit(int id)
        {
            return View(db.Jobs.Find(id));
        }

        public ActionResult JobDelete(int id)
        {
            return View(db.Jobs.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult JobAdd(Job job)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Jobs.Add(job);
                    db.SaveChanges();
                    return RedirectToAction("jobs");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult jobEdit(Job job)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(job).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("jobs");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost, ActionName("jobDelete")]
        [ValidateAntiForgeryToken]
        public ActionResult jobDeleteConfirmed(int id)
        {
            try
            {
                Job job = new Job() { Id = id };
                db.Entry(job).State = EntityState.Deleted;
                db.SaveChanges();
            }
            catch
            {
                return RedirectToAction("jobdelete",
                    new System.Web.Routing.RouteValueDictionary { 
                        { "id", id }, 
                        { "saveChangesError", true } });
            }

            return RedirectToAction("jobs");
        }

        // Ad
        public ActionResult Ads()
        {
            var ads = db.Ads;
            return View(ads);
        }

        public ActionResult AdAdd()
        {
            return View();
        }

        public ActionResult AdEdit(int id)
        {
            return View(db.Ads.Find(id));
        }

        public ActionResult AdDelete(int id)
        {
            return View(db.Ads.Find(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AdAdd(Ad ad, HttpPostedFileBase Image)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(Image.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/ads"), fileName);
                        Image.SaveAs(path);
                        ad.Image = fileName;
                        ad.CreationDate = DateTime.UtcNow;
                        db.Ads.Add(ad);
                        db.SaveChanges();
                        return RedirectToAction("ads");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Empty file.");
                        return View();
                    }
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AdEdit(Ad ad, HttpPostedFileBase Image)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(Image.FileName);
                        var path = Path.Combine(Server.MapPath("~/static/img/ads"), fileName);
                        Image.SaveAs(path);
                        ad.Image = fileName;
                    }

                    db.Entry(ad).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ads");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Unable to save changes.");
            }

            return View();
        }

        [HttpPost, ActionName("AdDelete")]
        [ValidateAntiForgeryToken]
        public ActionResult AdDeleteConfirmed(int id)
        {
            Ad ad = db.Ads.Find(id);

            try
            {
                var img = ad.Image;
                var path = Path.Combine(Server.MapPath("~/static/img/ads"), img);
                System.IO.File.Delete(path);
            }
            catch { }

            try
            {
                db.Ads.Remove(ad);
                db.SaveChanges();
            }
            catch
            {
                return RedirectToAction("addelete",
                    new System.Web.Routing.RouteValueDictionary { 
                        { "id", id }, 
                        { "saveChangesError", true } });
            }

            return RedirectToAction("ads");
        }
    }
}
